package com.example.demo.practice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.demo.practice.devices.Inspection;

import org.w3c.dom.Text;

public class UserLogin extends AppCompatActivity {

    private TextView inspect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题栏

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_user_login);
        inspect = (TextView) findViewById(R.id.inspection);
        inspect.setOnClickListener(new TextView.OnClickListener(){
            public void onClick(View v) {
                Intent intent = new Intent(UserLogin.this, Inspection.class);
                startActivity(intent);
                finish();
            }
        });

        init();
    }

    private void init(){
        changeState();
    }


    /**
     * 显示登陆状态
     */
    private void changeState(){
        EditText editText_name= (EditText) findViewById(R.id.input_name);
        EditText editText_pass= (EditText) findViewById(R.id.input_password);

        editText_name.setOnFocusChangeListener(new Listener());
        editText_pass.setOnFocusChangeListener(new Listener());
    }

    /**
     * 注册事件
     */
    public void register(View view) {
        Intent intent=new Intent(UserLogin.this,Register.class);
        startActivity(intent);
    }

    /**
     * 登陆事件
     * @param view {View}
     */
    public void login(View view) {
        Intent intent=new Intent(UserLogin.this,Home.class);
        startActivity(intent);
        finish();
    }

    /**
     * 焦点改变监听器
     */
    private  class Listener implements View.OnFocusChangeListener{

        @Override
        public void onFocusChange(View view, boolean b) {
            if(!b){
                EditText editText= (EditText) findViewById(view.getId());
                String content=editText.getText().toString();
                String type=editText.getHint().toString();

                if(content.equals("")){
                    changeStateText(type+"为空");
                }else {
                    EditText editText_name= (EditText) findViewById(R.id.input_name);
                    EditText editText_pass= (EditText) findViewById(R.id.input_password);
                    if(!editText_name.getText().toString().equals("")&&!editText_pass.getText().toString().equals("")){
                        changeStateText("");
                    }
                }
            }

        }
    }

    /**
     * 改变提示文字
     */
    private void changeStateText(String text){
        TextView textView= (TextView) findViewById(R.id.state);
        textView.setText(text);
    }
}
